package com.we.web.servlet;

import com.we.entity.Category;
import com.we.entity.PageBean;
import com.we.entity.ResultInfo;
import com.we.entity.User;
import com.we.service.CategoryService;
import com.we.service.impl.CategoryServiceImpl;
import com.we.util.JsonUtils;
import org.apache.commons.lang.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description 分类请求处理
 * @Author cr7
 * @Date 2021-11-16 10:37
 * @Version V1.0
 */
@WebServlet("/category")
public class CategoryServlet extends BaseServlet {

    private CategoryService service = new CategoryServiceImpl();
    private ResultInfo info = new ResultInfo();

    //查询所有分类数量
    public void findCategoryCount(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int count = service.findCategoryCount();
        if (count < 0) {
            info.setFlag(false);
            info.setData("查询分类数量失败");
        } else {
            info.setData(true);
            info.setData(count);
        }
        JsonUtils.writeValue(info, resp);
    }

    //查询所有分类及其数量方法(分页)
    public void findAllCategoryByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String currentPageStr = req.getParameter("currentPage");
        String pageSizeStr = req.getParameter("pageSize");

        int currentPage = 0;//当前页码，如果不传递，则默认为第一页
        if (currentPageStr != null && currentPageStr.length() > 0) {
            currentPage = Integer.parseInt(currentPageStr);
        } else {
            currentPage = 1;
        }

        int pageSize = 0;//每页显示条数，如果不传递，默认每页显示5条记录
        if (pageSizeStr != null && pageSizeStr.length() > 0) {
            pageSize = Integer.parseInt(pageSizeStr);
        } else {
            pageSize = 6;
        }

        PageBean<Category> pb = service.findAllCategoryByPage(currentPage, pageSize);
        JsonUtils.writeValue(pb, resp);
    }

    //查询所有分类及其数量方法
    public void findAllCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List list = service.findAllCategory();
        if (list.isEmpty()) {
            info.setFlag(false);
            info.setErrorMsg("当前无分类信息");
        } else {
            info.setFlag(true);
            info.setData(list);
        }
        JsonUtils.writeValue(info, resp);
    }

    //添加分类方法
    public void addCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = (User) req.getSession().getAttribute("user");
        if (!user.getIsAuthor().equals("root") || user == null) {
            info.setFlag(false);
            info.setErrorMsg("您没有相应的权限");
            JsonUtils.writeValue(info, resp);
            return;
        }

        String categoryName = req.getParameter("categoryName");
        if (StringUtils.isBlank(categoryName)) {
            info.setFlag(false);
            JsonUtils.writeValue(info, resp);
            return;
        }

        int count = service.addCategory(categoryName);
        if (count > 0) {
            info.setFlag(true);
        } else {
            info.setFlag(false);
            info.setErrorMsg("添加失败");
        }

        JsonUtils.writeValue(info, resp);
    }

    //根据分类名查询分类方法
    public void findCategoryByCategoryName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String categoryName = req.getParameter("categoryName");
        if (StringUtils.isBlank(categoryName)) {
            info.setFlag(false);
            JsonUtils.writeValue(info, resp);
            return;
        }

        int count = service.findCategoryByCategoryName(categoryName);
        if (count > 0) {
            info.setFlag(false);
            info.setErrorMsg("分类名重复，请检查");
        } else {
            info.setFlag(true);
        }

        JsonUtils.writeValue(info, resp);
    }

    //根据标签名删除标签
    public void deleteCategoryByCategoryName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        User user = (User) req.getSession().getAttribute("user");
        if (!user.getIsAuthor().equals("root") || user == null) {
            info.setFlag(false);
            info.setErrorMsg("您没有相应的权限");
            JsonUtils.writeValue(info, resp);
            return;
        }

        String categoryName = req.getParameter("categoryName");
        if (StringUtils.isBlank(categoryName)) {
            info.setFlag(false);
            info.setErrorMsg("分类名不能为空");
            JsonUtils.writeValue(info, resp);
            return;
        }

        int count = service.deleteCategoryByCategoryName(categoryName);
        if (count > 0) {
            info.setFlag(true);
        } else {
            info.setFlag(false);
            info.setErrorMsg("删除失败");
        }

        JsonUtils.writeValue(info, resp);
    }

    //根据分类名修改标签方法
    public void updateCategoryName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = (User) req.getSession().getAttribute("user");
        if (!user.getIsAuthor().equals("root") || user == null) {
            info.setFlag(false);
            info.setErrorMsg("您没有相应的权限");
            JsonUtils.writeValue(info, resp);
            return;
        }

        String categoryName = req.getParameter("categoryName");
        String categoryName1 = req.getParameter("categoryName1");
        if (StringUtils.isBlank(categoryName) || StringUtils.isBlank(categoryName1)) {
            info.setFlag(false);
            info.setErrorMsg("分类名不能为空");
            JsonUtils.writeValue(info, resp);
            return;
        }

        int count = service.updateCategoryName(categoryName, categoryName1);
        if (count < 0) {
            info.setFlag(false);
            info.setErrorMsg("修改失败");
        } else {
            info.setFlag(true);
        }

        JsonUtils.writeValue(info, resp);
    }


}
